@use "page";
@use "ct-card";
@import "global-variables";
@import "@patternfly/patternfly/utilities/Text/text.scss";

// utilities for `pf-u...` classes
@import "@patternfly/patternfly/utilities/Spacing/spacing.css";

// colors for PF charts
@import "@patternfly/patternfly/patternfly-charts.css";
@import "@patternfly/patternfly/patternfly-charts-theme-dark.css";

#app {
  section.pf-v5-c-page__main-breadcrumb {
    padding-block-end: var(--pf-v5-c-page__main-breadcrumb--PaddingTop);
  }
}

html {
  --metrics-heading-time-color: var(--pf-v5-global--BackgroundColor--light-100);
  --resource-icon-color-empty: var(--pf-v5-global--palette--black-400);
  --resource-icon-color-middle: var(--pf-v5-global--palette--orange-200);
  --resource-icon-color-full: var(--pf-v5-global--palette--orange-400);
}

.pf-v5-theme-dark {
  --metrics-heading-time-color: var(--pf-v5-global--BackgroundColor--300);
  --metrics-events-border-color: #000;
  --resource-icon-color-empty: var(--pf-v5-global--palette--black-400);
  --resource-icon-color-middle: var(--pf-v5-global--palette--orange-500);
  --resource-icon-color-full: var(--pf-v5-global--palette--orange-200);

  // Adjust alert color to separate itself from the history section background
  .pf-v5-c-alert {
    --pf-v5-c-alert--m-inline--m-info--BackgroundColor: var(--pf-v5-global--BackgroundColor--dark-400);
  }
}

// Remove doubled-up padding on "main" sections, when two+ sections are used.
.pf-v5-c-page__main-section + .pf-v5-c-page__main-section {
  padding-block-start: 0;
}

// FIXME: More styles from ct-system-overview
.current-metrics {
  --card-width: 200px;
  --pf-v5-l-gallery--GridTemplateColumns: repeat(auto-fit, minmax(var(--card-width), 1fr));

  // the cards already have padding, align the "top 5" tables with the other content
  .pf-v5-c-table {
    --pf-v5-c-table--m-compact--cell--first-last-child--PaddingLeft: 0;
    --pf-v5-c-table--m-compact--cell--first-last-child--PaddingRight: 0;

    tr > * {
      --pf-v5-c-table--cell--PaddingTop: var(--pf-v5-global--spacer--xs);
      --pf-v5-c-table--cell--PaddingBottom: var(--pf-v5-global--spacer--xs);
    }
  }

  .pf-v5-c-card {
    // Make sure the cards do not get taller than 50% of the screen
    // ...and when they do, they should scroll
    @media screen and (orientation: landscape) {
      max-block-size: 50vh;
      overflow: auto;
    }

    &__title-text {
      font-size: var(--pf-v5-global--FontSize--xl);
      font-weight: var(--pf-v5-global--FontWeight--normal);
    }

    // Let the children get flexy
    &__body {
      display: flex;
      flex-direction: column;

      // Strip the padding from the first item,
      // including table headings
      > :first-child,
      > :first-child > thead > tr > th {
        padding-block-start: 0;
      }

      .pf-v5-c-table thead th:not(:first-child),
      .pf-v5-c-table tbody td:not(:first-child) {
        text-align: end;
      }
    }

    .align-baseline {
      align-items: baseline;
    }

    .text-color-warning {
      color: var(--pf-v5-global--warning-color--200);
    }

    .icon-color-warning {
      color: var(--pf-v5-global--warning-color--100);
    }

    .text-color-critical {
      color: var(--pf-v5-global--danger-color--200);
    }

    .icon-color-critical {
      color: var(--pf-v5-global--danger-color--100);
    }

    .progress-stack {
      display: grid;
      padding-block-end: var(--pf-v5-global--spacer--sm);
      grid-gap: var(--pf-v5-global--spacer--sm) var(--pf-v5-global--spacer--md);

      &:not(:first-child) {
        padding-block-start: var(--pf-v5-global--spacer--sm);
      }
    }

    .progress-stack-no-space {
      display: grid;
      padding-block-end: var(--pf-v5-global--spacer--sm);
      grid-gap: 0;

      &:not(:first-child) {
        padding-block-start: var(--pf-v5-global--spacer--sm);
      }

      // avoid the "x" icon on high CPU usage
      .pf-v5-c-progress__status-icon {
        display: none;
      }

      // don't center the "View all CPUs" button
      button {
        padding-inline-start: 0;
        inline-size: fit-content;
      }
    }

    // Stretch the progress stack and description list so they fill up remaining space.
    // This pushes the (unflexing) tables to the bottom.
    .progress-stack,
    .pf-v5-c-description-list {
      flex: 1 1 0;
      align-content: start;
    }

    // When progress stack and description list are used sequentially,
    // give the most space to the latter of the two elements.
    .progress-stack + .pf-v5-c-description-list,
    .pf-v5-c-description-list + .progress-stack {
      flex-basis: 100%;
    }

    // WebKit is a little wonky with flex sizing; it needs specific instructions here
    .pf-v5-c-description-list:first-child {
      flex-grow: 0;
    }

    .pf-v5-c-description-list__description {
      font-size: var(--pf-v5-global--FontSize--sm);
    }
  }

  // Shrink progress bars and their gap a little
  .pf-v5-c-progress {
    grid-gap: 0;
    --pf-v5-c-progress__bar--Height: var(--pf-v5-global--spacer--xs);
    // PF4 uses end; it probably should arguably use last baseline.
    // (perhaps end may work better for non-text content?)
    align-items: last baseline;

    + .pf-v5-c-progress {
      // Add a tiny gap for sandwiched usage bars (as in CPU)
      padding-block-start: 1px;
    }
  }

  // Only underline the description
  .pf-v5-c-button.pf-m-link.pf-m-inline:hover {
    text-decoration: none;

    .pf-v5-c-progress__description {
      text-decoration: var(--pf-v5-c-button--m-link--m-inline--hover--TextDecoration);
    }
  }

  // Match font weight with description list terms
  .pf-v5-c-progress__description {
    font-weight: var(--pf-v5-global--FontWeight--bold);
  }

  // Recolor text
  .pf-m-link {
    &, &:hover {
      .pf-v5-c-progress__measure {
        color: var(--pf-v5-global--Color--100);
      }
    }
  }

  // default is way too wide for the cards
  .pf-v5-c-description-list {
    --pf-v5-c-description-list--m-horizontal__term--width: minmax(0, auto);
    --pf-v5-c-description-list--m-horizontal__description--width: minmax(0, auto);
  }

  // Don't wrap network in / out data
  // Shrink network cards and data a little more
  // (as space is costly and they use a lot)
  // FIXME: Let's fix the content (split off units) and drop this when we can
  .network-nowrap-shrink {
    td {
      white-space: nowrap;
      font-size: var(--pf-v5-global--FontSize--xs);
    }
  }

  // Shrink percentage column to allow for more service text
  th[data-label="%"],
  td[data-label="%"] {
    inline-size: 2rem;
    min-inline-size: 0;

    .pf-v5-c-table__text {
      --pf-v5-c-table--cell--MinWidth: 0;
      display: inline;
    }
  }

  .pf-m-grid-lg.pf-v5-c-table {
    tbody:first-of-type {
      border-block-start: none;
    }

    tbody tr:not(.pf-v5-c-table__expandable-row) {
      --pf-v5-c-table-tr--responsive--PaddingTop: var(--pf-v5-global--spacer--sm);
      --pf-v5-c-table-tr--responsive--PaddingRight: 0;
      --pf-v5-c-table-tr--responsive--PaddingBottom: var(--pf-v5-global--spacer--sm);
      --pf-v5-c-table-tr--responsive--PaddingLeft: 0;
      --pf-v5-c-table-tr--responsive--border-width--base: 1px;

      &:last-child {
        border-block-end: none;
      }

      > * {
        --pf-v5-c-table--cell--responsive--PaddingTop: 0;
        --pf-v5-c-table--cell--responsive--PaddingBottom: 0;
      }
    }
  }

  .all-disks-no-gap {
    // don't center the "View all disks" button
    button {
      padding-inline-start: 0;
      inline-size: fit-content;
    }
  }
}

.pf-v5-c-popover {
  thead th:not(:first-child) {
    text-align: end;
  }

  .disks-nowrap {
    td {
      white-space: nowrap;
    }

    td:not(:first-child) {
      text-align: end;
    }
  }
}

@media (min-width: 641px) and (max-width: 1200px) {
  .current-metrics {
    --card-width: 200px;
  }

  .current-metrics .pf-v5-c-card__body {
    overflow-block: auto;
    max-block-size: 50vw;
  }

  // Trim down the gutter space between cards
  .current-metrics.pf-v5-l-gallery.pf-m-gutter {
    grid-gap: var(--pf-v5-global--spacer--sm);
  }

  // Shave off a few pixels from cards
  .pf-v5-c-card__header, .pf-v5-c-card__title, .pf-v5-c-card__body, .pf-v5-c-card__footer {
    --pf-v5-c-card--first-child--PaddingTop: var(--pf-v5-global--spacer--sm);
    --pf-v5-c-card__title--not--last-child--PaddingBottom: var(--pf-v5-global--spacer--sm);
    --pf-v5-c-card--child--PaddingTop: var(--pf-v5-global--spacer--sm);
    --pf-v5-c-card--child--PaddingRight: var(--pf-v5-global--spacer--md);
    --pf-v5-c-card--child--PaddingBottom: var(--pf-v5-global--spacer--sm);
    --pf-v5-c-card--child--PaddingLeft: var(--pf-v5-global--spacer--md);
  }
}

// Change progress layout in medium sizes
@media (min-width: 460px) and (max-width: 1200px) {
  // Stack description lists (instead of grid)
  .pf-v5-c-description-list__group {
    display: block;
  }
}

// Stretch network card and reflow items
@media (min-width: 660px) and (max-width: 860px) {
  .current-metrics-network {
    grid-column: 1 / -1;

    // Reflow interfaces horizontally, with wrapping
    .pf-v5-c-table > tbody {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
      grid-gap: 0 var(--pf-v5-global--spacer--lg);
    }
  }
}

$graphs: cpu, memory, disks, network;

.metrics {
  --cpu-color: var(--pf-v5-chart-color-purple-200);
  --cpu-color-extreme: var(--pf-v5-chart-color-purple-300);

  --memory-color: var(--pf-v5-chart-color-green-200);
  --memory-color-extreme: var(--pf-v5-chart-color-green-300);

  --disks-color: var(--pf-v5-chart-color-gold-300);
  --disks-color-extreme: var(--pf-v5-chart-color-gold-400);

  --network-color: var(--pf-v5-chart-color-black-300);
  --network-color-extreme: var(--pf-v5-chart-color-black-400);

  --column-size: 10vw;
  --half-column-size: 8vw;
  --data-min-height: 5px;

  --graph-column-size: 60%;

  .pf-v5-theme-dark & {
    --cpu-color: var(--pf-v5-chart-color-purple-500);
    --cpu-color-extreme: var(--pf-v5-chart-color-purple-300);

    --memory-color: var(--pf-v5-chart-color-green-500);
    --memory-color-extreme: var(--pf-v5-chart-color-green-400);

    --disks-color: var(--pf-v5-chart-color-gold-500);
    --disks-color-extreme: var(--pf-v5-chart-color-gold-300);

    --network-color: var(--pf-v5-chart-color-black-400);
    --network-color-extreme: var(--pf-v5-chart-color-black-300);
  }

  &-hour-compressed {
    --data-min-height: 1px;
    display: flex;
    background-color: var(--pf-v5-global--BackgroundColor--light-300);
  }

  &-heading,
  &-hour:not(.metrics-hour-compressed) .metrics-minute {
    background-color: var(--pf-v5-global--BackgroundColor--light-300);
    display: flex;
  }

  &-heading,
  &-heading-graphs {
    align-items: center;
  }

  &-graphs {
    flex-basis: var(--graph-column-size);
    margin-inline-start: auto;
    display: grid;
    grid-template-columns: repeat(var(--graph-cnt), minmax(var(--column-size), 1fr));

    .metrics-data {
      min-block-size: var(--data-min-height);
    }
  }

  &-sublabels.pf-v5-c-content {
    display: flex;

    small {
      font-size: var(--pf-v5-global--FontSize--xs);
      margin-block-end: var(--pf-v5-global--spacer--xs) !important;
    }
  }

  &-label-graph {
    text-align: end;

    & .metrics-sublabels {
      justify-content: flex-end;
    }
  }

  &-label-graph.have-saturation {
    text-align: center;

    & .metrics-sublabels {
      justify-content: space-evenly;
    }
  }

  &-minutes {
    flex-basis: var(--graph-column-size);
    align-self: end;
  }

  &-events-hour-header-expanded {
    inline-size: calc(100% - var(--graph-column-size));
  }

  &-selectors,
  &-events-wrapper {
    flex-basis: calc(100% - var(--graph-column-size));
    inline-size: 100%;
  }

  &-events {
    display: flex;
    column-gap: var(--pf-v5-global--spacer--sm);
    flex-basis: calc(100% - var(--graph-column-size));
    align-items: baseline;
    font-size: var(--pf-v5-global--FontSize--sm);

    &-expander {
      min-inline-size: 3rem;
    }

    time:first-child {
      margin-inline-start: calc(3rem + var(--pf-v5-global--spacer--sm));

      + .spikes_count {
        padding-inline-start: calc(var(--pf-v5-global--spacer--xl) + var(--pf-v5-global--spacer--md));
        color: var(--pf-v5-global--Color--200);
      }
    }

    .spikes_count {
      > div {
        white-space: nowrap;
        align-items: baseline;
      }

      svg {
        align-self: center;
      }
    }

    > * {
      line-height: var(--pf-v5-global--LineHeight--md);
    }

    > *:not(.metrics-events-expander):not(:last-child) {
      flex: 1;
      white-space: nowrap;
    }

    > *:last-child {
      flex-basis: 50%;
    }

    // Just embolden the top level time events
    button + time {
      font-weight: var(--pf-v5-global--FontWeight--bold);
    }

    // Make messages such as `Loading...` or `No logs found` centered
    .cockpit-log-panel {
      display: flex;
      flex-wrap: wrap;
      // justify-content: center;

      .cockpit-logline {
        flex: 0 0 100%;
      }
    }
  }

  &-time,
  &-heading {
    padding-inline: var(--pf-v5-c-page__main-section--PaddingLeft) var(--pf-v5-c-page__main-section--PaddingRight);
  }

  &-time {
    background: var(--metrics-heading-time-color);
    padding-block: var(--pf-v5-global--spacer--md) var(--pf-v5-global--spacer--sm);

    // Time has tabular numbers, but it's not what we want for a heading
    time {
      font-variant: none !important;
    }
  }

  &-history-section {
    padding: 0;
  }

  &-history {
    background-color: var(--pf-v5-global--BackgroundColor--light-300);
    padding: 0;
  }

  &-data {
    --gradient-fragment: var(--color) 80%, var(--color-extreme) 90%;
    --gradient: linear-gradient(to right, var(--gradient-fragment));
    --gradient-flip: linear-gradient(to left, var(--gradient-fragment));
    display: grid;
    contain: strict;
    margin-block: 0;
    margin-inline: var(--pf-v5-global--spacer--sm);
    // Make SVG and generated before/after line position relative to metrics-data
    position: relative;
    grid-template-areas: "utilization";
    // Make sure rows expand (WebKit needs this)
    grid-template-rows: 1fr;

    // vertical ruler line for areas with data, and horizontal tick mark for utilization half
    &.valid-data::before {
      content: "";
      position: absolute;
      z-index: 1;
      inline-size: 100%;
      inset-block: 0;
      border-inline-end: 1px solid rgb(0 0 0 / 30%);
    }

    &.valid-data.have-saturation::before {
      inline-size: 50%;
    }

    // horizontal tick mark for saturation half, for resources that have it
    &.valid-data.have-saturation::after {
      content: "";
      position: absolute;
      z-index: 1;
      inline-size: 50%;
      inset-inline-start: 50%;
    }

    .polygon {
      // WebKit has an issue with normal positioning (height exceeds bounds), but absolute works fine
      position: absolute;
      inset-block-start: 0;
      inset-inline-start: 0;
      clip-path: polygon(var(--points));
      background: var(--gradient);
      block-size: 100%;
      inline-size: 100%;
    }

    @each $graph in $graphs {
      &-#{$graph} {
        --color: var(--#{$graph}-color);
        --color-extreme: var(--#{$graph}-color-extreme);
      }
    }

    /* we always need to flip the polygons in Y direction, as the SVG
     * coordinate system's Y goes downwards, and the computed SVGs are relative
     * to (0,0), with a non-constant max-Y.
     * Flip them in X direction for RTL. */

    .polygon.polygon-use {
      grid-area: utilization;
      // horizontally the use graphs go to the left on LTR, to the right on RTL
      transform: scale(-1, -1);
      [dir="rtl"] & { transform: scale(1, -1); }
    }

    .polygon.polygon-sat {
      grid-area: saturation;
      opacity: 0.7;
      // horizontally the load graphs go to the right on LTR, to the left on RTL
      transform: scale(1, -1);
      [dir="rtl"] & { transform: scale(-1, -1); }
    }

    .compressed.polygon-use {
      background: var(--gradient-flip);
      clip-path: inset(0 0 0 calc(100% - var(--utilization) * 100%));
      [dir="rtl"] & { transform: scale(-1, 1); }
    }

    .compressed.polygon-sat {
      background: var(--gradient);
      clip-path: inset(0 calc(100% - var(--saturation) * 100%) 0 0);
      opacity: 0.7;
      [dir="rtl"] & { transform: scale(-1, 1); }
    }
  }

  &-data.have-saturation {
    grid-template-areas: "utilization saturation";
  }

  // vertical dotted line through graph center, for areas without data
  &-data::after {
    content: "";
    z-index: -1;
    inset-block-start: 0;
    inset-block-end: 0;
    position: absolute;
  }

  &-data.empty-data::after {
    position: initial;
    border-inline-end: 1px dotted rgb(0 0 0 / 30%);
  }

  &-data.empty-data.have-saturation::after {
    position: absolute;
    border-inline-start: 1px dotted rgb(0 0 0 / 30%);
    border-inline-end: none;
    inset-inline-start: 50%;
  }

  &-hour {
    padding-block: 0;
    padding-inline: var(--pf-v5-global--spacer--sm);
  }

  &-hour:not(.metrics-hour-compressed) .metrics-minute,
  &-hour.metrics-hour-compressed .metrics-minute:last-of-type {
    .valid-data.have-saturation::after,
    .valid-data::before {
      // Fake a border via clip-path, to set it on top, instead of after
      background: rgb(120 120 120 / 30%);
      pointer-events: none;
      clip-path: inset(calc(100% - 1px) 0 0);
    }
  }

  .pf-v5-c-card__body:first-child {
    padding-block-start: 0;
  }
}

// Graph column labels are the widest part; scale down the labels for narrow widths
@media (min-width: 800px) and (max-width: 1200px) {
  .metrics-label-graph {
    font-size: var(--pf-v5-global--FontSize--sm);
  }
}

@media (min-width: 800px) {
  .metrics-label-graph-mobile {
    display: none;
  }
}

@media (max-width: 800px) {
  .metrics {
    --graph-column-size: 40%;

    time:first-child {
      + .spikes_count {
        padding-inline-start: 0;
      }
    }
  }

  .metrics-heading > .metrics-graphs,
  .metrics-minute > .metrics-events > .spikes_count {
    display: none;
  }

  .metrics-label-graph-mobile {
    padding-inline-start: var(--pf-v5-global--spacer--sm);
    padding-block-start: var(--pf-v5-global--spacer--sm);
    font-size: var(--pf-v5-global--FontSize--sm);
  }

  .square {
    inline-size: 1rem;
    block-size: 1rem;
  }

  @each $graph in $graphs {
    .label-#{$graph} {
      background-color: var(--#{$graph}-color);
    }
  }

  .metrics-hour > .metrics-events {
    .spikes_info,
    .spikes_count svg {
      display: none;
    }
  }
}

.bottom-panel {
  display: flex;
  justify-content: space-around;
  padding: 1em;
}

.nodata {
  margin: 1em;
}

.select-min {
  inline-size: min-content;
}

#pcp-settings-modal {
  .pcp-settings-modal-text {
    // "Read more..." link
    a {
      padding-inline-start: 1em;
      white-space: nowrap;
    }
  }

  // leave some space for the focus rings
  .pf-v5-c-switch {
    margin-block-end: 10px;
  }
}

.cpu-all {
  --pf-v5-l-grid--m-gutter--GridGap: var(--pf-v5-global--spacer--xs) var(--pf-v5-global--spacer--md);
  // Mixing font families need to be aligned properly (to the baseline)
  align-items: baseline;
  // In monospaced text, "100%" is 4 characters
  grid-template-columns: 1fr minmax(4ch, auto);

  > .pf-v5-l-grid__item {
    // Don't use PF's hardcoded 12-column grid
    grid-column: auto;
  }

  > dd {
    // TODO: Use tabular numbers instead of monospace,
    // after new font usage is enabled
    font-family: var(--pf-v5-global--FontFamily--monospace);
    // Aligning percentages to the right, especially when monospaced,
    // makes  it easier to see high percentages
    text-align: end;
  }
}

.current-cpu-usage:not(:last-child) {
  // Bump CPU average above CPU max
  z-index: 1;

  // Make the background transparent, so CPU max shows through
  .pf-v5-c-progress__bar {
    --pf-v5-c-progress__bar--BackgroundColor: transparent;
    --pf-v5-c-progress__bar--before--Opacity: 0;
  }
}

.current-top-cpu-usage {
  // Nudge max to be at the same place as average
  margin-block-start: -5px;

  // Lighten up the default bar
  &:not(.pf-m-danger):not(.pf-m-warning) {
    .pf-v5-c-progress__indicator {
      opacity: 0.33;
    }
  }
}

// FIXME: We probably want to adapt some of these for the general log message widget
.cockpit-logline {
  padding-block: 0.25rem;
  padding-inline: 0;
}

.cockpit-log-message {
  text-align: start;
  overflow-wrap: anywhere;
  white-space: break-spaces;
  // Clamping is supported in all browsers,
  // but it needs special webkit prefix (yes, in all browsers)
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  display: webkit-box; /* stylelint-disable-line */
  color: var(--pf-v5-global--active-color--100);
}

// Log warning icon is incorrectly aligned (especially when messages wrap);
// this fixes it by realigning and offsetting
.cockpit-log-warning {
  transform: translateY(calc((var(--pf-v5-global--spacer--md) - 22px) / -2));
  margin: 0;
  margin-inline-end: var(--pf-v5-global--spacer--xs);
  align-self: baseline;
}

.metrics-events-popover {
  .cockpit-log-panel {
    overflow: auto;
    // Remove extra whitespace; the modal already has some
    padding: 0;
    margin: 0;
  }

  .pf-v5-c-popover__content {
    // Remove extra whitespace in the modal
    --pf-v5-c-popover--c-button--sibling--PaddingRight: 0;
  }
}

@media (min-width: 801px) {
  .metrics-events-popover {
    .cockpit-log-panel {
      // Limit the size of the popover to make it more elegant
      max-block-size: 65vw;
    }

    .pf-v5-c-popover__content {
      // Limit the width of the popover in desktop mode
      max-inline-size: max(30rem, 40vw);
    }
  }
}

@media (max-width: 800px) {
  .metrics-events-popover {
    // Make the popover into a sheet from the bottom in mobile mode
    inset-block: auto 0 !important;
    inset-inline: 0 !important;
    transform: none !important;
    max-block-size: 100%;
    min-block-size: 25vh;

    .pf-v5-c-popover__content {
      // Limit the width of the popover in desktop mode
      max-inline-size: max(100vw);
    }

    // As a sheet at the bottom, it doesn't point to anything
    .pf-v5-c-popover__arrow {
      display: none;
    }
  }
}

/* Menu toggles are transparent in light mode (but not in dark
   mode). This makes them white on a white page (fine), but grey in a
   grey toolbar. The deprecated Select had a white background. Let's
   make MenuToggles match.
*/
html:not(.pf-v5-theme-dark) .pf-v5-c-menu-toggle:not(.pf-m-primary) {
    background-color: var(--pf-v5-global--BackgroundColor--100);
}
